package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.BompTreeNodeConstant;
import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.constant.TableNameConstant;
import cc.rengu.igas.bomp.common.dao.*;
import cc.rengu.igas.bomp.common.dao.impl.*;
import cc.rengu.igas.bomp.common.entity.*;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.util.ChangeNextStatusUtil;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.facade.enums.BompMchntNatureEnum;
import cc.rengu.igas.channel.alipay.facade.bean.AddressInfo;
import cc.rengu.igas.channel.alipay.facade.bean.BankCardInfo;
import cc.rengu.igas.channel.alipay.facade.bean.ContactInfo;
import cc.rengu.igas.channel.alipay.facade.request.AliCreateMerchantRequest;
import cc.rengu.igas.channel.alipay.facade.request.AliUpdateMerchantRequest;
import cc.rengu.igas.channel.alipay.facade.response.AliCreateMerchantResponse;
import cc.rengu.igas.channel.alipay.facade.response.AliUpdateMerchantResponse;
import cc.rengu.igas.channel.wechat.facade.request.WechatCreateMerchantRequest;
import cc.rengu.igas.channel.wechat.facade.request.WechatUpdateMerchantRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatCreateMerchantResponse;
import cc.rengu.igas.channel.wechat.facade.response.WechatUpdateMerchantResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.TxnInfo;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.TxnInfoService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.TxnInfoServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/*
 * @version 1.0
 * @date 2019/08/15
 */
public class AsynInstMchntRegisterService extends RadpService {

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        rglog.info("异步进行商户入驻开始");
        try {
            WechatMchntInfo wechatMchntInfo = (WechatMchntInfo) xmlTreeUtil.getXmlTreeObjectValue(BompTreeNodeConstant.WECHAT_MCHNT_INFO, WechatMchntInfo.class);
            AlipayMchntInfo alipayMchntInfo = (AlipayMchntInfo) xmlTreeUtil.getXmlTreeObjectValue(BompTreeNodeConstant.ALIPAY_MCHNT_INFO, AlipayMchntInfo.class);
            String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
            if (null == wechatMchntInfo && null == alipayMchntInfo) {
                rglog.info("微信商户与支付宝商户为空，异步进行商户入驻结束");
                return 0;
            }

            MchntBaseInfo mchntBaseInfo = (MchntBaseInfo) xmlTreeUtil.getXmlTreeObjectValue(BompTreeNodeConstant.MCHNT_BASE_INFO, MchntBaseInfo.class);
            MchntSignInfo mchntSignInfo = (MchntSignInfo) xmlTreeUtil.getXmlTreeObjectValue(BompTreeNodeConstant.MCHNT_SIGN_INFO, MchntSignInfo.class);

            TxnInfo txnInfo = (TxnInfo) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.TXN_INFO, TxnInfo.class);

            //微信商户进件或修改商户信息
            wechatCreateMerchant(xmlTreeUtil, wechatMchntInfo, sysSeqNum, mchntBaseInfo, txnInfo);

            boolean alipayInDirectFlag = txnInfo.getChannelCallCfgList().stream().anyMatch(item ->
                    item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()));
            if (alipayInDirectFlag) {
                //间联模式，支付宝商户进件或修改商户信息
                alipayCreateMerchant(xmlTreeUtil, alipayMchntInfo, sysSeqNum, mchntBaseInfo, mchntSignInfo, txnInfo);
            } else {
                rglog.info("直联支付宝进件需要先获取商户应用授权，请在内管平台上点击支付宝进件按钮完成博时进件申请!");
            }
        } catch (Exception e) {
            rglog.error(e.getMessage());
            rglog.error("登记虚拟账户记账支付订单失败");
        }

        return 0;
    }

    private void alipayCreateMerchant(XmlTreeUtil xmlTreeUtil, AlipayMchntInfo alipayMchntInfo, String sysSeqNum, MchntBaseInfo mchntBaseInfo, MchntSignInfo mchntSignInfo, TxnInfo txnInfo) throws Exception {
        //支付宝进件
        if (null != alipayMchntInfo && StringUtil.isEmptyOrNull(alipayMchntInfo.getAlipayMchntNo())) {
            //判断当前状态是否允许修改
            if (!ChangeNextStatusUtil.isAllowModifyData(alipayMchntInfo.getAuditStatus())) {
                rglog.error("第三方机构商户进件处理失败, 当前支付宝商户信息待审核，不允许进行进件操作，instId:{},mchntNo:{},当前商户审核状态:{}",
                        alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), alipayMchntInfo.getAuditStatus());
                throw new BizException(RespCodeEnum.MANAGE_OPERATE_ERROR.getRespCode(), RespCodeEnum.MANAGE_OPERATE_ERROR.getRespDesc());
            }

            //获取交易码映射关系- 支付宝
            //获取交易码映射关系-微信
            List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) ||
                    item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.ALIP.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg channelCallCfg = channelCallCfgList.get(0);

            //调用支付宝进行商户入驻
            AliCreateMerchantRequest aliCreateMerchantRequest = initAliCreateMerchantRequest(sysSeqNum, mchntBaseInfo, alipayMchntInfo, mchntSignInfo, channelCallCfg);
            DubboService dubboService = new DubboServiceImpl();
            Object object = dubboService.callDubboService(DubboMethodConstant.DUBBO_ALIPAY_MCHNT_SERVICE, "aliCreateMerchant", aliCreateMerchantRequest);
            cc.rengu.igas.channel.alipay.facade.result.Result<AliCreateMerchantResponse> aliResult = (cc.rengu.igas.channel.alipay.facade.result.Result<AliCreateMerchantResponse>) object;
            if (null != aliResult && aliResult.isSuccess() && DubboMethodConstant.DUBBO_SUCCESS.equals(aliResult.getResult().getRespCode())) {
                //更新商户支付宝信息表与历史数据表 入驻成功
                alipayMchntInfo.setAlipayMchntNo(aliResult.getResult().getAliMchntNo());
                alipayMchntInfo.setAlipayEntryFlag("01");
                BizHistory bizHistory = new BizHistory();
                BeanUtil.beanCopy(alipayMchntInfo, bizHistory);
                bizHistory.setManageInstId(mchntBaseInfo.getSignInstId());
                bizHistory.setMchntName(mchntBaseInfo.getMchntName());
                bizHistory.setRealId(alipayMchntInfo.getId());
                bizHistory.setOprTableEname(TableNameConstant.T_B_ALPAY_MCHNT_INFO);
                //修改
                bizHistory.setBizOpr("U");
                String modifyAliMchntInfoJson = JSONObject.toJSONString(alipayMchntInfo);

                //将正式表的商户信息存入历史表 bizData
                InitParamUtil.initBizHistoryData(bizHistory, modifyAliMchntInfoJson);
                modifyAliMchntInfoData(xmlTreeUtil, alipayMchntInfo, bizHistory);
            } else {
                rglog.error("第三方机构商户进件处理失败, 当前微信商户信息进件失败，instId:{},mchntNo:{},当前商户审核状态:{}",
                        alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), alipayMchntInfo.getAuditStatus());
                //更新商户支付宝信息表与历史数据表 入驻失败
                alipayMchntInfo.setAlipayEntryFlag("02");
                BizHistory bizHistory = new BizHistory();
                BeanUtil.beanCopy(alipayMchntInfo, bizHistory);
                bizHistory.setManageInstId(mchntBaseInfo.getSignInstId());
                bizHistory.setMchntName(mchntBaseInfo.getMchntName());
                bizHistory.setRealId(alipayMchntInfo.getId());
                bizHistory.setOprTableEname(TableNameConstant.T_B_ALPAY_MCHNT_INFO);
                //修改
                bizHistory.setBizOpr("U");
                String modifyAliMchntInfoJson = JSONObject.toJSONString(alipayMchntInfo);

                //将正式表的商户信息存入历史表 bizData
                InitParamUtil.initBizHistoryData(bizHistory, modifyAliMchntInfoJson);
                modifyAliMchntInfoData(xmlTreeUtil, alipayMchntInfo, bizHistory);
            }
        } else if (null != alipayMchntInfo) {
            List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) ||
                    item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.ALIP.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg channelCallCfg = channelCallCfgList.get(1);

            //调用支付宝进行商户修改
            AliCreateMerchantRequest aliCreateMerchantRequest = initAliCreateMerchantRequest(sysSeqNum, mchntBaseInfo, alipayMchntInfo, mchntSignInfo, channelCallCfg);
            AliUpdateMerchantRequest aliUpdateMerchantRequest = new AliUpdateMerchantRequest();
            //数据一致
            BeanUtil.beanCopy(aliCreateMerchantRequest, aliUpdateMerchantRequest);
            aliUpdateMerchantRequest.setHeader(aliCreateMerchantRequest.getHeader());
            DubboService dubboService = new DubboServiceImpl();
            Object object = dubboService.callDubboService(DubboMethodConstant.DUBBO_ALIPAY_MCHNT_SERVICE, "aliUpdateMerchant", aliUpdateMerchantRequest);
            cc.rengu.igas.channel.alipay.facade.result.Result<AliUpdateMerchantResponse> aliResult = (cc.rengu.igas.channel.alipay.facade.result.Result<AliUpdateMerchantResponse>) object;
            if (null == aliResult || !aliResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(aliResult.getResult().getRespCode())) {
                rglog.error("支付宝商户更新商户信息失败,instId:{},mchntNo:{}", alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo());
            }

        }
    }

    private void wechatCreateMerchant(XmlTreeUtil xmlTreeUtil, WechatMchntInfo wechatMchntInfo, String sysSeqNum, MchntBaseInfo mchntBaseInfo, TxnInfo txnInfo) throws Exception {
        //微信进件
        if (null != wechatMchntInfo && StringUtil.isEmptyOrNull(wechatMchntInfo.getWechatMchntNo())) {
            //判断当前状态是否允许修改
            if (!ChangeNextStatusUtil.isAllowModifyData(wechatMchntInfo.getAuditStatus())) {
                rglog.error("第三方机构商户进件处理失败, 当前微信商户信息待审核，不允许进行进件操作，instId:{},mchntNo:{},当前商户审核状态:{}",
                        wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getAuditStatus());
                throw new BizException(RespCodeEnum.MANAGE_OPERATE_ERROR.getRespCode(), RespCodeEnum.MANAGE_OPERATE_ERROR.getRespDesc());
            }

            //获取交易码映射关系-银联微信
            List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPWX.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg channelCallCfg = channelCallCfgList.get(0);

            WechatCreateMerchantRequest wechatCreateMerchantRequest = initWechatCreateMerchantRequest(sysSeqNum, wechatMchntInfo, channelCallCfg, mchntBaseInfo);
            DubboService dubboService = new DubboServiceImpl();
            Result<WechatCreateMerchantResponse> wechatResult = (Result<WechatCreateMerchantResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "createMerchant", wechatCreateMerchantRequest);
            if (null == wechatResult || !wechatResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(wechatResult.getResult().getRespCode())) {
                rglog.error("第三方机构商户进件处理失败, 当前微信商户信息进件失败，instId:{},mchntNo:{},当前商户审核状态:{}",
                        wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getAuditStatus());
                //更新商户微信信息表与历史数据表 更新入驻失败
                wechatMchntInfo.setWechatEntryFlag("02");
                BizHistory bizHistory = new BizHistory();
                BeanUtil.beanCopy(wechatMchntInfo, bizHistory);
                bizHistory.setManageInstId(mchntBaseInfo.getSignInstId());
                bizHistory.setMchntName(mchntBaseInfo.getMchntName());
                bizHistory.setRealId(wechatMchntInfo.getId());
                bizHistory.setOprTableEname(TableNameConstant.T_B_WECHAT_MCHNT_INFO);
                //修改
                bizHistory.setBizOpr("U");
                String modifyWechatMchntInfoJson = JSONObject.toJSONString(wechatMchntInfo);

                //将正式表的商户信息存入历史表 bizData
                InitParamUtil.initBizHistoryData(bizHistory, modifyWechatMchntInfoJson);
                modifyWechatMchntInfoData(xmlTreeUtil, wechatMchntInfo, bizHistory);
            } else {
                //更新商户微信信息表与历史数据表
                wechatMchntInfo.setWechatMchntNo(wechatResult.getResult().getThirdMchntNo());
                wechatMchntInfo.setWechatEntryFlag("01");
                wechatMchntInfo.setLastEntryTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
                BizHistory bizHistory = new BizHistory();
                BeanUtil.beanCopy(wechatMchntInfo, bizHistory);
                bizHistory.setManageInstId(mchntBaseInfo.getSignInstId());
                bizHistory.setMchntName(mchntBaseInfo.getMchntName());
                bizHistory.setRealId(wechatMchntInfo.getId());
                bizHistory.setOprTableEname(TableNameConstant.T_B_WECHAT_MCHNT_INFO);
                //修改
                bizHistory.setBizOpr("U");
                String modifyWechatMchntInfoJson = JSONObject.toJSONString(wechatMchntInfo);

                //将正式表的商户信息存入历史表 bizData
                InitParamUtil.initBizHistoryData(bizHistory, modifyWechatMchntInfoJson);
                modifyWechatMchntInfoData(xmlTreeUtil, wechatMchntInfo, bizHistory);
            }

        } else if (null != wechatMchntInfo) {
            //更新微信商户信息
            //判断当前状态是否允许修改
            if (!ChangeNextStatusUtil.isAllowModifyData(wechatMchntInfo.getAuditStatus())) {
                rglog.error("第三方机构商户进件处理失败, 当前微信商户信息待审核，不允许进行进件操作，instId:{},mchntNo:{},当前商户审核状态:{}",
                        wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getAuditStatus());
                throw new BizException(RespCodeEnum.MANAGE_OPERATE_ERROR.getRespCode(), RespCodeEnum.MANAGE_OPERATE_ERROR.getRespDesc());
            }

            //获取交易码映射关系-银联微信
            List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPWX.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg channelCallCfg = channelCallCfgList.get(1);

            WechatUpdateMerchantRequest wechatUpdateMerchantRequest = new WechatUpdateMerchantRequest();
            BeanUtil.beanCopy(wechatMchntInfo, wechatUpdateMerchantRequest);
            cc.rengu.igas.channel.wechat.facade.base.Header header = new cc.rengu.igas.channel.wechat.facade.base.Header();
            header.setChanlId(ChannelEnum.BOMP.getChannelType());
            header.setSrcSysId(AppParamConstant.SYS_ID);
            header.setTxnNum(channelCallCfg.getCallChannelTxn());
            header.setVersion("1.0.0");
            String channelSeqNum = sysSeqNum + channelCallCfg.getCallIndex();
            header.setTraceNo(channelSeqNum);
            header.setTransTime(DateUtil.getCurrentTime());
            header.setTransDate(DateUtil.getCurrentDate());
            wechatUpdateMerchantRequest.setHeader(header);
            wechatUpdateMerchantRequest.setChannelId(channelCallCfg.getCallChannelId());
            //商户产品信息表的微信支付通道编码
            wechatUpdateMerchantRequest.setChannelType(wechatMchntInfo.getDstChannelType());
            wechatUpdateMerchantRequest.setThirdMchntNo(wechatMchntInfo.getWechatMchntNo());
            wechatUpdateMerchantRequest.setMchntAbbr(mchntBaseInfo.getMchntSimpleName());
            wechatUpdateMerchantRequest.setServicePhone(mchntBaseInfo.getMchntContactPhone());
            DubboService dubboService = new DubboServiceImpl();
            Result<WechatUpdateMerchantResponse> wechatResult = (Result<WechatUpdateMerchantResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "updateMerchant", wechatUpdateMerchantRequest);
            if (null == wechatResult || !wechatResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(wechatResult.getResult().getRespCode())) {
                rglog.error("第三方机构商户信息修改, 当前微信商户信息修改失败，instId:{},mchntNo:{},当前商户审核状态:{}",
                        wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getAuditStatus());
            }
        }
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }

    private WechatCreateMerchantRequest initWechatCreateMerchantRequest(String sysSeqNum,
                                                                        WechatMchntInfo wechatMchntInfo,
                                                                        ChannelCallCfg channelCallCfg,
                                                                        MchntBaseInfo mchntBaseInfo) throws Exception {

        WechatCreateMerchantRequest wechatCreateMerchantRequest = new WechatCreateMerchantRequest();
        BeanUtil.beanCopy(wechatMchntInfo, wechatCreateMerchantRequest);
        cc.rengu.igas.channel.wechat.facade.base.Header header = new cc.rengu.igas.channel.wechat.facade.base.Header();
        header.setChanlId(ChannelEnum.BOMP.getChannelType());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setVersion("1.0.0");
        String channelSeqNum = sysSeqNum + channelCallCfg.getCallIndex();
        header.setTraceNo(channelSeqNum);
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        wechatCreateMerchantRequest.setHeader(header);
        wechatCreateMerchantRequest.setChannelId(channelCallCfg.getCallChannelId());
        //商户产品信息表的微信支付通道编码
        wechatCreateMerchantRequest.setChannelType(wechatMchntInfo.getDstChannelType());
        wechatCreateMerchantRequest.setMchntNo(mchntBaseInfo.getMchntNo());

        //默认对公
        String publicPrivateKey = "01";
        BompMchntNatureEnum wechatType = BompMchntNatureEnum.getBompMchntNatureEnumByType(mchntBaseInfo.getMchntNature());
        if (null != wechatType) {
            //根据商户性质判断对公对私
            publicPrivateKey = wechatType.getWechatPublicPrivateKey();
        }
        IndustryMapInfoMapper industryMapInfoMapper = new IndustryMapInfoMapperImpl();
        IndustryMapInfo wechatIndustry = industryMapInfoMapper.selectIndustryMapInfoByIndustryTypeAndChannelType(mchntBaseInfo.getIndustryType(), ChannelEnum.WXZF.getChannelType(), publicPrivateKey);
        if (null == wechatIndustry) {
            //默认使用对公再次查询，若还查无数据则失败
            wechatIndustry = industryMapInfoMapper.selectIndustryMapInfoByIndustryTypeAndChannelType(mchntBaseInfo.getIndustryType(), ChannelEnum.WXZF.getChannelType(), "01");
            if (null == wechatIndustry) {
                rglog.error("通过行业类型查询微信行业类型映射关系失败！，instId:{},mchntNo:{},industryType:{}",
                        mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), mchntBaseInfo.getIndustryType());
                throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
            }
        }

        //商户经营类目
        wechatCreateMerchantRequest.setBusiness(wechatIndustry.getDestCatalogCd());
        wechatCreateMerchantRequest.setMchntName(mchntBaseInfo.getMchntName());
        wechatCreateMerchantRequest.setMchntAbbr(mchntBaseInfo.getMchntSimpleName());
        wechatCreateMerchantRequest.setServicePhone(mchntBaseInfo.getMchntContactPhone());
        wechatCreateMerchantRequest.setContact(mchntBaseInfo.getMchntContactName());
        wechatCreateMerchantRequest.setContactPhone(mchntBaseInfo.getMchntContactMobile());
        wechatCreateMerchantRequest.setContactEmail(mchntBaseInfo.getMchntContactEmail());

        return wechatCreateMerchantRequest;
    }

    private AliCreateMerchantRequest initAliCreateMerchantRequest(String sysSeqNum,
                                                                  MchntBaseInfo mchntBaseInfo,
                                                                  AlipayMchntInfo alipayMchntInfo,
                                                                  MchntSignInfo mchntSignInfo,
                                                                  ChannelCallCfg channelCallCfg) throws Exception {

        MchntAcctInfoMapper mchntAcctInfoMapper = new MchntAcctInfoMapperImpl();
        List<MchntAcctInfo> mchntAcctInfoList = mchntAcctInfoMapper.selectMchntAcctInfoByMchntNo(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());


        AliCreateMerchantRequest aliCreateMerchantRequest = new AliCreateMerchantRequest();
        cc.rengu.igas.channel.alipay.facade.base.Header header = new cc.rengu.igas.channel.alipay.facade.base.Header();
        BeanUtil.beanCopy(alipayMchntInfo, aliCreateMerchantRequest);
        header.setChanlId(ChannelEnum.BOMP.getChannelType());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setVersion("1.0.0");
        String channelSeqNum = sysSeqNum + channelCallCfg.getCallIndex();
        header.setTraceNo(channelSeqNum);
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        aliCreateMerchantRequest.setHeader(header);
        aliCreateMerchantRequest.setMchntName(mchntBaseInfo.getMchntName());
        //商户MCC
        aliCreateMerchantRequest.setMchntType(mchntBaseInfo.getIndustryType());
        aliCreateMerchantRequest.setMchntAbbr(mchntBaseInfo.getMchntSimpleName());
        aliCreateMerchantRequest.setAlipayCategory(alipayMchntInfo.getAlipayCategory());
        //客服电话
        aliCreateMerchantRequest.setServicePhone(mchntBaseInfo.getMchntContactPhone());
        aliCreateMerchantRequest.setChannelId(channelCallCfg.getCallChannelId());
        aliCreateMerchantRequest.setChannelType(alipayMchntInfo.getDstChannelType());

        //联系人信息
        ContactInfo contactInfo = new ContactInfo();
        contactInfo.setContact(mchntSignInfo.getArtifName());
        contactInfo.setContactTelephone(mchntSignInfo.getArtifMobile());
        contactInfo.setContactPhone(mchntSignInfo.getArtifMobile());
        contactInfo.setContactEmail(mchntSignInfo.getArtifEmail());
        contactInfo.setContactJobType("06");
        contactInfo.setContactType("LEGAL_PERSON");
        contactInfo.setCertId(mchntSignInfo.getArtifCertNo());
        aliCreateMerchantRequest.setContactInfoList(Collections.singletonList(contactInfo));

        //地址信息
        AddressInfo addressInfo = new AddressInfo();
        addressInfo.setCityCode(mchntSignInfo.getRegisterCity());
        addressInfo.setDistrictCode(mchntSignInfo.getRegisterArea());
        addressInfo.setAddress(mchntBaseInfo.getMchntBizAddr());
        addressInfo.setProvinceCode(mchntSignInfo.getRegisterProvince());
        aliCreateMerchantRequest.setAddressInfoList(Collections.singletonList(addressInfo));

        List<BankCardInfo> bankCardInfoList = new ArrayList<>();
        mchntAcctInfoList.forEach(item -> convertBankCardInfo(item, bankCardInfoList));
        aliCreateMerchantRequest.setBankCardInfoList(bankCardInfoList);
        return aliCreateMerchantRequest;
    }

    /*
     * 将数据库表结构对象转换成接口对象
     *
     * @param mchntAcctInfo    数据库表结构对象
     * @param bankCardInfoList 接口对象
     */
    private void convertBankCardInfo(MchntAcctInfo mchntAcctInfo, List<BankCardInfo> bankCardInfoList) {
        if (BompAppParamConstant.FLAG_Y.equals(mchntAcctInfo.getRecordStatus())) {
            BankCardInfo bankCardInfo = new BankCardInfo();
            bankCardInfo.setAcctNo(mchntAcctInfo.getSettleAcctNo());
            bankCardInfo.setAcctName(mchntAcctInfo.getSettleAcctName());
            bankCardInfo.setBankName(mchntAcctInfo.getAcctOpenBankName());
            bankCardInfoList.add(bankCardInfo);
        }
    }


    private void modifyWechatMchntInfoData(XmlTreeUtil xmlTreeUtil, WechatMchntInfo wechatMchntInfo, BizHistory bizHistory) throws Exception {
        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        int dbResult = wechatMchntInfoMapper.updateWechatMchntInfoById(wechatMchntInfo);
        if (dbResult != 0) {
            dbsUtil.dbsEndTransaction(false);
            rglog.error("微信商户进件-修改微信商户信息表失败！id:{},mchnt:{},instId:{}",
                    wechatMchntInfo.getId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getInstId());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }


        if (null != bizHistory) {
            BizHistoryMapper bizHistoryMapper = new BizHistoryMapperImpl();
            dbResult = bizHistoryMapper.insertBompBizHistory(bizHistory);
            if (dbResult != 0) {
                dbsUtil.dbsEndTransaction(false);
                rglog.error("微信商户进件-新增业务历史表记录失败！realId:{},manageInstId:{},instId:{}",
                        bizHistory.getRealId(), bizHistory.getManageInstId(), bizHistory.getInstId());
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        }

        dbsUtil.dbsEndTransaction(true);
    }


    private void modifyAliMchntInfoData(XmlTreeUtil xmlTreeUtil, AlipayMchntInfo alipayMchntInfo, BizHistory bizHistory) throws Exception {
        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        AlipayMchntInfoMapper alipayMchntInfoMapper = new AlipayMchntInfoMapperImpl();
        int dbResult = alipayMchntInfoMapper.updateAlipayMchntInfoById(alipayMchntInfo);
        if (dbResult != 0) {
            dbsUtil.dbsEndTransaction(false);
            rglog.error("支付宝商户进件-修改支付宝商户信息表失败！id:{},mchnt:{},instId:{}",
                    alipayMchntInfo.getId(), alipayMchntInfo.getMchntNo(), alipayMchntInfo.getInstId());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        if (null != bizHistory) {
            BizHistoryMapper bizHistoryMapper = new BizHistoryMapperImpl();
            dbResult = bizHistoryMapper.insertBompBizHistory(bizHistory);
            if (dbResult != 0) {
                dbsUtil.dbsEndTransaction(false);
                rglog.error("支付宝商户进件-新增业务历史表记录失败！realId:{},manageInstId:{},instId:{}",
                        bizHistory.getRealId(), bizHistory.getManageInstId(), bizHistory.getInstId());
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        }

        dbsUtil.dbsEndTransaction(true);
    }
}
